package com.ruoyi.onlyOffice.service.impl;

import com.ruoyi.onlyOffice.domain.CheckFormInfo;
import com.ruoyi.onlyOffice.domain.FileUpload;
import com.ruoyi.onlyOffice.mapper.FileUploadMapper;
import com.ruoyi.onlyOffice.service.FileUploadService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class FileUploadServiceImpl implements FileUploadService {
    @Resource
    FileUploadMapper  uploadMapper;

    @Override
    public List<FileUpload> list(String taskId) {
        return uploadMapper.selectFileList(taskId);
    }

    @Override
    public void saveFile(FileUpload upload) {
        uploadMapper.saveFile(upload);
    }

    @Override
    public List<FileUpload> queryFileByName(String name) {
        FileUpload fileUpload = new FileUpload();
        fileUpload.setFileName(name);
        return uploadMapper.queryFileList(fileUpload);
    }

    @Override
    public void updateFile(List<FileUpload> fileUploadList,String taskId) {
        if (fileUploadList != null && fileUploadList.size() > 0){
            for (FileUpload fileUpload : fileUploadList) {
                fileUpload.setUploadDate(new Date());
                fileUpload.setTaskId(taskId);
                uploadMapper.updateByName(fileUpload);
            }
        }
    }

    public void updateFileInfo(List<FileUpload> fileUploadList,String taskId, String fieldKey) {
        if (fileUploadList != null && fileUploadList.size() > 0){
            for (FileUpload fileUpload : fileUploadList) {
                fileUpload.setUploadDate(new Date());
                fileUpload.setTaskId(taskId);
                fileUpload.setFieldKey(fieldKey);
                uploadMapper.updateByName(fileUpload);
            }
        }
    }

    public List<FileUpload> queryFileList(String taskId,Long userId,String fieldKey) {
        FileUpload fileUpload = new FileUpload();
        fileUpload.setUserId(userId);
        fileUpload.setTaskId(taskId);
        fileUpload.setFieldKey(fieldKey);
        return uploadMapper.queryFileList(fileUpload);
    }

    @Override
    public List<CheckFormInfo> selectFormList(String taskId) {
        return uploadMapper.selectFormList(taskId);
    }

    @Override
    public void saveForm(CheckFormInfo formInfo) {
        uploadMapper.saveForm(formInfo);
    }
}
